<?php

namespace app\admin\model\advertiser;

use app\BaseModel;
use think\model\relation\HasOne;

/**
 * 广告主页面广告位授权表
 */
class AdvertiserPageUser extends BaseModel
{

    /**
     * 页面广告位授权广告主列表
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $orderby
     * @return array
     */
    public static function getList(array $where, int $page, int $limit, string $orderby = ''): array
    {
        $data = self::where($where)
            ->with(['advertiserUser' => function ($query) {
                $query->field('id,name');
            }])
            ->order($orderby)
            ->paginate(['list_rows' => $limit, 'page' => $page], false)
            ->toArray();
        //遍历数组
        foreach ($data['data'] as $k => &$v) {
            $v['device_type_arr'] = json_decode($v['device_type'], true);
        }
        return $data;
    }

    /**
     * 关联广告主表
     * @return HasOne
     */
    public function advertiserUser(): HasOne
    {
        return $this->hasOne(AdvertiserUser::class, 'id', 'ad_user_id');
    }

    /**
     * 关联广告主页面广告表
     * @return HasOne
     */
    public function advertiserPage(): HasOne
    {
        return $this->hasOne(AdvertiserPage::class, 'id', 'page_id');
    }

}